﻿/*----------------------------------------------------------------------------
 * osObjects.h: CMSIS-RTOS global object definitions for an application
 *----------------------------------------------------------------------------
 *
 * This header file defines global RTOS objects used throughout a project
 *
 * #define osObjectsPublic indicates that objects are defined; without that
 * definition the objects are defined as external symbols.
 *
 *--------------------------------------------------------------------------*/


#ifndef __osObjects
#define __osObjects


#ifdef osObjectsCreat
#define OBJ_EXT  
#else
#define OBJ_EXT extern 
#endif

#include "common.h"
#include "cmsis_os.h"
#include "mem_alloc.h"

//#include "cmsis_os.h"               // CMSIS RTOS header file

#define OS_TICK_FREQ       TX_TIMER_TICKS_PER_SECOND
#define ms_ticks(x)        (x*OS_TICK_FREQ/1000)

//os 内部变量
extern TX_THREAD *     _tx_thread_current_ptr;
#define thread_now    _tx_thread_current_ptr

extern char * tx_err_txt[];

#ifdef DEBUG
void os_err_print(uint16_t os_err, char *file, int line);
#define os_api_exe_check() os_err_print(os_err, __FILE__, __LINE__)
#else
#define os_api_exe_check()
#endif

// global 'thread' functions ---------------------------------------------------

int os_thread_init(void);
int os_obj_creat(void);
int os_thread_creat(void);

/*
********************************************************************************
*                       任务优先级，数值越小优先级越高
********************************************************************************
*/
#define  APP_CFG_TASK_START_PRIO                          2u
#define  APP_CFG_TASK_USER_IF_PRIO                        4u
#define  APP_CFG_TASK_STAT_PRIO                           28u
#define  APP_CFG_TASK_IDLE_PRIO                           31u

#define  APP_CFG_TASK_CONSOLE_PRIO                        23u
#define  APP_CFG_TASK_SYSTEM_PRIO                         6u
#define  APP_CFG_TASK_OSAPI_PRIO                          25u
#define  APP_CFG_TASK_MODBUS_PRIO                         5u
#define  APP_CFG_TASK_FILEX_PRIO                          22u


/*
********************************************************************************
*                                静态全局变量
********************************************************************************
*/

typedef struct
{
  osThreadId  tid_system;


  osThreadId  tid_modbus;



  TX_MUTEX   mutex_printf;	/* 用于printf互斥 */
  osMutexId  mid_printf;

  TX_MUTEX   mutex_common;	/* 用于全局公共资源保护 */
  osMutexId  mid_common;

  TX_QUEUE   quene_tst;
  uint32_t   quene_tst_mem[64];
  osQueneId  qid_tst;

  TX_EVENT_FLAGS_GROUP event_tst;
  osEventId  eid_tst;

  TX_EVENT_FLAGS_GROUP event_mb;
  osEventId  eid_mb;

  TX_SEMAPHORE sem_tst;
  osSemaphoreId sid_tst;

  // thread id
  osThreadId  tid_StorageTask;                              // thread id
  osThreadId  tid_console;

  // global 'semaphores' -----------------------------------------------------
  osMutexId mid_q25;

  // global 'message queues' -------------------------------------------------


  // global 'mail queues' ----------------------------------------------------

  TX_BYTE_POOL mem_pool;
  TX_BYTE_POOL * pid_mem;


} os_obj_type;

OBJ_EXT os_obj_type  os_obj;                                  // thread id


#endif  // __osObjects
